﻿-- Get the LibTweetCraft library
local LibTweetCraft = LibStub:GetLibrary("LibTweetCraft-1.2");

-- Register the addon
local AddOn = LibTweetCraft:RegisterAutoTweetAddOn("Achievements");

function AddOn:Initialize()
	-- Add the categories
	AddOn:AddCategory("Achievement", "Earn an Achievement", "Automatically tweets when your character earns a new achievement.");
	AddOn:AddCategory("AchievementScreenshot", "Take Achievement Screenshot", "Automatically takes a screenshot when your character earns a new achievement.", false);

	-- Register for events
	AddOn.EventHandler:RegisterEvent("ACHIEVEMENT_EARNED")
end

function AddOn.EventHandler:ACHIEVEMENT_EARNED(achievementId)
	-- Get the name of the player's character
	local unitName = UnitName("player")

	-- Get the name of the realm
	local realmName = GetRealmName()
	
	-- Get the achievement information
	local id, name = GetAchievementInfo(achievementId)
	
	-- Tweet the achievement
	AddOn:Tweet("Achievement", unitName .. " has earned the " .. name .. " achievement on " .. realmName .. ". #wow")
	
	-- Check whether AchievementScreenshot is enabled
	if AddOn:IsCategoryEnabled("AchievementScreenshot") then
		TweetCraft:Wait(1,
		function()
			print("Screenshot of achievement taken.")
			Screenshot()
		end)
	end
end
